package com.controller;

import com.alibaba.fastjson.JSON;

import com.common.AjaxResult;
import com.common.RequestMappingAnalysis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: 谢韬元
 * 日期: 2024/12/14
 * 类名: DipatchServlet
 */

@WebServlet(name = "DipatchServlet", urlPatterns = "/")
public class DipatchServlet extends HttpServlet {

    Map<String, String> urlMapping = new HashMap<String, String>();

    @Override
    public void init() throws ServletException {
        super.init();
//        urlMapping.put("/cart/list", "com.controller.CartServlet.getList");
//        urlMapping.put("/cart/add", "com.controller.CartServlet.add");
//        urlMapping.put("/cart/del", "com.controller.CartServlet.del");
//        urlMapping.put("/cart/update", "com.controller.CartServlet.update");

        try {
            urlMapping.putAll(RequestMappingAnalysis.getRequestMapping("com.controller"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (resp.getContentType() == null) {
            // 设置响应数据的内容类型为application/json;
            resp.setContentType("application/json;charset=UTF-8");
        }

        System.out.println("进入DipatchServlet");
        String urlStr = req.getRequestURI();
        //通过反射获取到class实例，再调用相应的方法
        String urlValue = urlMapping.get(urlStr);

        PrintWriter out = resp.getWriter();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(500);
        if (urlValue == null) {

            ajaxResult.setCode(404);
            ajaxResult.setMsg("您访问的路径不存在");
            out.print(JSON.toJSONString(ajaxResult));
        }
        String className = urlValue.substring(0, urlValue.lastIndexOf("."));
        String methodName = urlValue.substring(urlValue.lastIndexOf(".") + 1);

        try {

            Class clzz = Class.forName(className);

            Object o = clzz.getConstructor().newInstance();

            Method method = clzz.getMethod(methodName);

            Object result = method.invoke(o);
            ajaxResult.setCode(200);
            ajaxResult.setResult(result);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        out.print(JSON.toJSONString(ajaxResult));

    }
}
